package dk.iha;

import android.content.Context;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GPSSensor implements ISensor{
	
	LocationManager locationManager;
	LocationListener locationListener;
	Context ctx;
	Position position = null;
	boolean Running = false;

	public GPSSensor(Context ctx) {
		//SharedData.getInstance().addDebugDataLog_t("GPS initialized");
		this.ctx = ctx;
		position = new Position();
		locationManager = (LocationManager) this.ctx.getSystemService(Context.LOCATION_SERVICE);
		statusChanged();
	}
	
	public Position getPosition() {
		//SharedData.getInstance().addDebugDataLog_t("getPosition():"+position.toString());
		return position;
	}

	public void start() {
		//SharedData.getInstance().addDebugDataLog_t("GPS STARTED!!!");
		Running=true;

		locationListener = new LocationListener() {
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// called when the location provider status changes.
				// Possible status: OUT_OF_SERVICE,
				// TEMPORARILY_UNAVAILABLE or AVAILABLE.
			}

			public void onProviderEnabled(String provider) {
				// called when the location provider is enabled by the
				// user
			}

			public void onProviderDisabled(String provider) {
				// called when the location provider is disabled by the
				// user. If it is already disabled, it's called
				// immediately after requestLocationUpdates
			}

			public void onLocationChanged(Location location) {
				position.setLatitude(location.getLatitude());
				position.setLongitude(location.getLongitude());
				//SharedData.getInstance().addDebugDataLog_t("onLocationChanged:"+position);
			}
		};
		//request GPS updates
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,	0, locationListener);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,	0, locationListener);


//		if (latitude == 0 || longitude == 0) {
//			Location location = locationManager
//					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//			if (location != null) {
//				latitude = location.getLatitude();
//				longitude = location.getLongitude();
//			}
//			SharedData.getInstance().addDebugDataLog_t(
//					"position unknown - getting last known position- latitude: "
//							+ latitude + " longitude: " + longitude);
//		}

	}

	public void statusChanged() {
		locationManager.addGpsStatusListener(new GpsStatus.Listener() {

			public void onGpsStatusChanged(int event) {
				switch (event) {
				// Event sent when the GPS system has started
				case GpsStatus.GPS_EVENT_STARTED:
					//SharedData.getInstance().addDebugDataLog_t("GPS Started");
					break;

				// Event sent when the GPS system has stopped
				case GpsStatus.GPS_EVENT_STOPPED:
					//SharedData.getInstance().addDebugDataLog_t("GPS Stopped");
					break;

				// Event sent when the GPS system has received its first fix
				// since starting
				case GpsStatus.GPS_EVENT_FIRST_FIX:
					//SharedData.getInstance().addDebugDataLog_t("GPS got first fix:"+position);
					break;

				// Event sent periodically to report GPS satellite status
				case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
					// Do nothing...
					break;

				}
			}
		});
	}

	public void stop() {
		locationManager.removeUpdates(locationListener);
		//SharedData.getInstance().addDebugDataLog_t("GPS STOPPED!!!");
		Running=false;
	}

	public boolean isRunning() {
		return Running;
	}

}
